package com.bsoft.config;

import com.bsoft.message.Demo06Message;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitConfig {
    /**
     * direct Exchanges 示例的配置类
     */
    public static class DirectExchangesDemoConfiguration{
        //创建Queue
        @Bean
        public Queue demo06Queue(){
            return  new Queue(Demo06Message.QUEUE, // queue名称
                    true,  //是否持久化

                    false, //是否排它
                    false); //是否自动删除
        }

        //创建 direct demo06Queue
        @Bean
        public DirectExchange demo06Exchange(){
            return  new DirectExchange(Demo06Message.EXCHANGE,
                    true, //是否持久化
                     false
                    ); // 是否排它
        }
       @Bean
        public Binding demo06Binding(){
            return BindingBuilder.bind(demo06Queue()).to(demo06Exchange()).with(Demo06Message.ROUTING_KEY);
        }

        @Bean(name = "consumerBatchContainerFactory")
        public SimpleRabbitListenerContainerFactory consumerBatchContainerFactory(
                SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
            // 创建 SimpleRabbitListenerContainerFactory 对象
            SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
            configurer.configure(factory, connectionFactory);
            // 额外添加批量消费的属性
            factory.setBatchListener(true);
            // <X>
            factory.setBatchSize(10);
            factory.setReceiveTimeout(30 * 1000L);
            factory.setConsumerBatchEnabled(true);
            return factory;
        }

    }
}
